#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDockWidget>
#include <QPushButton>
#include <QLabel>
#include <QLayout>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //添加子窗口
    QDockWidget* dockWidget=new QDockWidget();
    this->addDockWidget(Qt::LeftDockWidgetArea,dockWidget);
    //给子窗口设置标题
    dockWidget->setWindowTitle("浮动窗口");
    //给子窗口添加控件
    //由于子窗口只能添加一个控件,要添加多个控件则需先创建一个widget
    //然后将要添加的控件先添加到widget中,最后将widget添加到子窗口中
    QWidget* widget=new QWidget();
    QPushButton* button=new QPushButton("按钮");
    QLabel* label= new QLabel("这是一个子窗口的标签");
    QVBoxLayout* layout=new QVBoxLayout();
    widget->setLayout(layout);
    layout->addWidget(button);
    layout->addWidget(label);
    dockWidget->setWidget(widget);

    //设置子窗口允许停靠位置
    dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::TopDockWidgetArea);
}

MainWindow::~MainWindow()
{
    delete ui;
}

